package com.homedo.async.mysql.support.convertor.forfieldval;

import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.data.Numeric;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author jgshun
 * @date 2020/3/10 3:27 PM
 */
public abstract class ResultFieldValConvertorUtil {
    public static Object convertor(Class fieldClz, Object columnVal) {
        if (columnVal == null) {
            return null;
        }

        if (String.class.isAssignableFrom(fieldClz)) {
            return StringResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Byte.class.isAssignableFrom(fieldClz)) {
            return ByteResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Short.class.isAssignableFrom(fieldClz)) {
            return ShortResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Integer.class.isAssignableFrom(fieldClz)) {
            return IntegerResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Long.class.isAssignableFrom(fieldClz)) {
            return LongResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Numeric.class.isAssignableFrom(fieldClz)) {
            return NumericResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Float.class.isAssignableFrom(fieldClz)) {
            return FloatResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Double.class.isAssignableFrom(fieldClz)) {
            return DoubleResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (LocalDate.class.isAssignableFrom(fieldClz)) {
            return LocalDateResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (LocalDateTime.class.isAssignableFrom(fieldClz)) {
            return LocalDateTimeResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Duration.class.isAssignableFrom(fieldClz)) {
            return DurationResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Buffer.class.isAssignableFrom(fieldClz)) {
            return BufferResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (JsonObject.class.isAssignableFrom(fieldClz)) {
            return JsonObjectResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (JsonArray.class.isAssignableFrom(fieldClz)) {
            return JsonArrayResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (BigDecimal.class.isAssignableFrom(fieldClz) && columnVal instanceof Numeric) {
            return BigDecimalResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Boolean.class.isAssignableFrom(fieldClz)) {
            return BooleanResultFieldValConvertor.getInstance().convertor(columnVal);
        } else if (Date.class.isAssignableFrom(fieldClz)) {
            return DateResultFieldValConvertor.getInstance().convertor(columnVal);
        }

        throw new RuntimeException("[未知类型" + fieldClz + ", " + columnVal + "]");
    }
}
